package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.v1.request.UserShoppingCartAddRequest;
import com.frogs.ucenter.v1.request.UserShoppingCartBatchAddRequest;
import com.frogs.ucenter.v1.request.UserShoppingCartDeleteRequest;
import com.frogs.ucenter.v1.request.UserShoppingCartListRequest;
import com.frogs.ucenter.v1.request.UserShoppingCartMergeRequest;
import com.frogs.ucenter.v1.request.UserShoppingCartSumRequest;
import com.frogs.ucenter.v1.request.UserShoppingCartTopListRequest;
import com.frogs.ucenter.v1.request.UserShoppingCartUpdateRequest;
import com.frogs.ucenter.v1.service.UserShoppingCartService;
import com.frogs.ucenter.v1.util.ApiConstants;

/**
 * 购物车接口
 * 注意：游客购物车独立出来，是为了绕开登录拦截器和方便以后游客和登录用户购物车的扩展
 * @author wushubin
 *
 */
@Controller(ApiConstants.VERSION+"userShoppingCartController")
@RequestMapping("/"+ApiConstants.VERSION)
public class UserShoppingCartController extends BaseController3 {
    private static final String ROOT_USER = "/user/shoppingCart";//登录用户购物车
    private static final String ROOT_VISITOR = "/visitor/shoppingCart";//游客购物车
    
    private static final String SUM = "/sum";//购物车中商品总数
    private static final String BATCH_ADD = "/batchAdd"; //批量添加购物车
    
    private static final String USER_SHOPPING_CART_LIST = ROOT_USER + LIST;
    private static final String USER_SHOPPING_CART_ADD = ROOT_USER + ADD;
    private static final String USER_SHOPPING_CART_UPDATE = ROOT_USER + UPDATE;
    private static final String USER_SHOPPING_CART_DELETE = ROOT_USER + DELETE;
    private static final String USER_SHOPPING_CART_SUM = ROOT_USER + SUM;
    private static final String USER_SHOPPING_CART_TOP_LIST = ROOT_USER + TOP_LIST;
    private static final String USER_SHOPPING_CART_BATCH_ADD = ROOT_USER + BATCH_ADD;
    private static final String USER_SHOPPING_CART_MERGE = ROOT_USER + MERGE;
    
    private static final String VISITOR_SHOPPING_CART_LIST = ROOT_VISITOR + LIST;
    private static final String VISITOR_SHOPPING_CART_ADD = ROOT_VISITOR + ADD;
    private static final String VISITOR_SHOPPING_CART_UPDATE = ROOT_VISITOR + UPDATE;
    private static final String VISITOR_SHOPPING_CART_DELETE = ROOT_VISITOR + DELETE;
    private static final String VISITOR_SHOPPING_CART_SUM = ROOT_VISITOR + SUM;
    private static final String VISITOR_SHOPPING_CART_TOP_LIST = ROOT_VISITOR + TOP_LIST;
    private static final String VISITOR_SHOPPING_CART_BATCH_ADD = ROOT_VISITOR + BATCH_ADD;
    
    
    @Resource(name=ApiConstants.VERSION+"userShoppingCartService")
    private UserShoppingCartService userShoppingCartService;

    @RequestMapping(VISITOR_SHOPPING_CART_LIST)
    public Response listVisitorShoppingCart(HttpServletRequest request, ModelMap model) {
    	return this.listShoppingCart(request, model);
    }
    
    @RequestMapping(VISITOR_SHOPPING_CART_ADD)
    public Response saveVisitorShoppingCart(HttpServletRequest request, ModelMap model) {
    	return this.saveShoppingCart(request, model);
    }
    
    @RequestMapping(VISITOR_SHOPPING_CART_UPDATE)
    public Response updateVisitorShoppingCart(HttpServletRequest request, ModelMap model) throws Exception {
    	return this.updateShoppingCart(request, model);
    }

    @RequestMapping(VISITOR_SHOPPING_CART_DELETE)
    public Response deleteVisitorShoppingCart(HttpServletRequest request, ModelMap model) {
        return this.deleteShoppingCart(request, model);
    }
    
    @RequestMapping(VISITOR_SHOPPING_CART_SUM)
    public Response sumVisitorShoppingCart(HttpServletRequest request, ModelMap model) {
        return this.sum(request, model);
    }
    
    @RequestMapping(VISITOR_SHOPPING_CART_TOP_LIST)
    public Response topVisitorShoppingCart(HttpServletRequest request, ModelMap model) {
        return this.topShoppingCart(request, model);
    }
    
    @RequestMapping(VISITOR_SHOPPING_CART_BATCH_ADD)
    public Response batchSaveVisitorShoppingCart(HttpServletRequest request, ModelMap model) {
    	return this.batchSaveShoppingCart(request, model);
    }
    
    @RequestMapping(USER_SHOPPING_CART_SUM)
    public Response sum(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserShoppingCartSumRequest sumRequest = new UserShoppingCartSumRequest();
        	sumRequest.parse(request);
        	response = userShoppingCartService.getSum(sumRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("error message" + e.getMessage());
			}
		}
        return response;
    }
    
    @RequestMapping(USER_SHOPPING_CART_TOP_LIST)
    public Response topShoppingCart(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserShoppingCartTopListRequest listRequest = new UserShoppingCartTopListRequest();
        	listRequest.parse(request);
        	response = userShoppingCartService.getTopList(listRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("error message" + e.getMessage());
			}
		}
        return response;
    }
    
    @RequestMapping(USER_SHOPPING_CART_LIST)
    public Response listShoppingCart(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserShoppingCartListRequest listRequest = new UserShoppingCartListRequest();
        	listRequest.parse(request);
        	response = userShoppingCartService.getPageList(listRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("error message" + e.getMessage());
			}
		}
        return response;
    }

    @RequestMapping(USER_SHOPPING_CART_ADD)
    public Response saveShoppingCart(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserShoppingCartAddRequest addRequest = new UserShoppingCartAddRequest();
        	addRequest.parse(request);
        	response = userShoppingCartService.insert(addRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("error message" + e.getMessage());
			}
		}
        return response;
    }

    @RequestMapping(USER_SHOPPING_CART_UPDATE)
    public Response updateShoppingCart(HttpServletRequest request, ModelMap model) throws Exception {
        //只修改数量
    	Response response = null ; 
        try{
        	UserShoppingCartUpdateRequest updateRequest = new UserShoppingCartUpdateRequest();
        	updateRequest.parse(request);
        	response = userShoppingCartService.update(updateRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("error message" + e.getMessage());
			}
		}
        return response;
    }

    @RequestMapping(USER_SHOPPING_CART_DELETE)
    public Response deleteShoppingCart(HttpServletRequest request, ModelMap model) {
        //只修改状态
        Response response = null ; 
        try{
        	UserShoppingCartDeleteRequest deleteRequest = new UserShoppingCartDeleteRequest();
        	deleteRequest.parse(request);
        	response = userShoppingCartService.delete(deleteRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("error message" + e.getMessage());
			}
		}
        return response;
    }
    
    @RequestMapping(USER_SHOPPING_CART_MERGE)
    public Response mergeShoppingCart(HttpServletRequest request, ModelMap model) {
        //只修改状态
        Response response = null ; 
        try{
        	UserShoppingCartMergeRequest mergeRequest = new UserShoppingCartMergeRequest();
        	mergeRequest.parse(request);
        	response = userShoppingCartService.merge(mergeRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("error message" + e.getMessage());
			}
		}
        return response;
    }
    
    @RequestMapping(USER_SHOPPING_CART_BATCH_ADD)
    public Response batchSaveShoppingCart(HttpServletRequest request, ModelMap model) {
    	Response response = null ; 
        try{
        	UserShoppingCartBatchAddRequest addRequest = new UserShoppingCartBatchAddRequest();
        	addRequest.parse(request);
        	response = userShoppingCartService.batchInsert(addRequest);
        }catch (Exception e) {
			response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("error message" + e.getMessage());
			}
		}
        return response;
    }
}